package com.ruoyi.yuce.controller;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.yuce.domain.EndStart;
import com.ruoyi.yuce.domain.pojo.CoBuJian;
import com.ruoyi.yuce.domain.pojo.CoChangZhan;
import com.ruoyi.yuce.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 查询开始时间和结束时间的Controller
 *
 * @author ruoyi
 * @date 2023-02-14
 */
@RestController
@RequestMapping("/ruoyi-yuce/endstart")
@DataSource(value = DataSourceType.SLAVELS)
public class EndStartController extends BaseController
{
    @Autowired
    private IEndStartService endStartService;

    @Autowired
    private IFengjiService fengjiService;

    /**
     * 查询故障结束和开始时间记录列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:endstart:list')")
    @GetMapping("/list")
    public TableDataInfo list(EndStart endStart) {
        startPage();
        Date date = Date.valueOf(endStart.getCriqi());
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String time="csczevent"+sf.format(date);
        endStart.setCriqi(time);
        List<EndStart> list = endStartService.selectEndStartList(endStart);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:endstart:getChangZhanName')")
    @GetMapping("/getChangZhanName")
    public List<CoChangZhan> getChangZhanName(EndStart endStart)
    {
        logger.info("getChangZhanName function ...");
        Date date = Date.valueOf(endStart.getCriqi());
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String time="csczevent"+sf.format(date);
        endStart.setCriqi(time);
        List<CoChangZhan> changZhanName = endStartService.getChangZhanName(endStart);
        return changZhanName;
    }

    /**
     * 查询部件
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:endstart:getBuJian')")
    @GetMapping("/getBuJian")
    public List<CoBuJian> getBuJian(EndStart endStart) {
        List<CoBuJian> buJian = fengjiService.getBuJian();
        return buJian;
    }
    /**
     * 查询部件
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:endstart:getBuJian1')")
    @GetMapping("/getBuJian1")
    public List<CoBuJian> getBuJian1(EndStart endStart) {
        logger.info("getBuJian function ...");
        Date date = Date.valueOf(endStart.getCriqi());
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String time="csczevent"+sf.format(date);
        endStart.setCriqi(time);
        List<CoBuJian> buJian = endStartService.getBuJian(endStart);
        return buJian;
    }
    /**
     * 导出遥测记录表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:endstart:export')")
    @Log(title = "故障记录表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EndStart endStart)
    {

        Date date = Date.valueOf(endStart.getCriqi());
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String time="csczevent"+sf.format(date);
        endStart.setCriqi(time);
        List<EndStart> list = endStartService.selectEndStartList(endStart);
        ExcelUtil<EndStart> util = new ExcelUtil<EndStart>(EndStart.class);
        util.exportExcel(response, list, "故障结束-开始时间");
    }


}
